package com.yjf.indoormap.utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

import com.yjf.indoormap.entity.Shop;

import java.util.ArrayList;
import java.util.UUID;

import jxl.Sheet;
import jxl.Workbook;

/**
 * Created by yif on 2017/4/22.
 * 数据导入类，从excel导入数据，插入到本地数据库中去
 * 获取 excel 表格中的数据,不能在主线程中调用
 */

public class DataImportUtils {

    private final String TAG = DataImportUtils.class.getSimpleName();
    private final String xlsName = "shop.xls";

    public ArrayList<Shop> getXlsData(Context context) {
        ArrayList<Shop> countryList = new ArrayList<>();
        AssetManager assetManager = context.getAssets();

        try {
            Workbook workbook = Workbook.getWorkbook(assetManager.open(xlsName));
            Sheet sheet = workbook.getSheet(0);

            int sheetNum = workbook.getNumberOfSheets();
            int sheetRows = sheet.getRows();
            int sheetColumns = sheet.getColumns();

            Log.d(TAG, "the num of sheets is " + sheetNum);
            Log.d(TAG, "the name of sheet is  " + sheet.getName());
            Log.d(TAG, "total rows is 行=" + sheetRows);
            Log.d(TAG, "total cols is 列=" + sheetColumns);

            for (int i = 1; i < sheetRows; i++) {
                Shop countryModel = new Shop();
                countryModel.setName(sheet.getCell(0, i).getContents());
                countryModel.setType(sheet.getCell(1, i).getContents());
                countryModel.setLevel(Integer.valueOf(sheet.getCell(2, i).getContents()));
                countryModel.setIcon(sheet.getCell(3, i).getContents());
                countryModel.setPointX(Float.valueOf(sheet.getCell(4, i).getContents()));
                countryModel.setPointY(Float.valueOf(sheet.getCell(5, i).getContents()));
                countryModel.setNode(sheet.getCell(6,i).getContents());
                countryModel.setRemark(sheet.getCell(7,i).getContents());
                countryModel.setIntroduction(sheet.getCell(8,i).getContents());
                countryModel.setGuid(UUID.randomUUID().toString());
                countryList.add(countryModel);
            }

            workbook.close();

        } catch (Exception e) {
            Log.e(TAG, "read error=" + e, e);
        }

        return countryList;
    }


}
